<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
//-- load base class if needed
require_once( APPPATH . 'controllers/base/adminbase.php' );

// --
class user extends adminbase {

    public function user() {
        parent::__construct();
        //-- load library
        $this->load->library("tnotification");
        $this->load->library("pagination");
        //-- load model
        $this->load->model("user/m_user");
    }

    public function index() {
        $this->page();
    }

    public function page() {
        //-- set rule
        $this->setPageRule("R");
        
        //-- bisnis proses
        //-- url
        $this->smarty->assign("strUrlList", site_url("user/user"));
        $this->smarty->assign("strUrlAdd", site_url("user/user/add"));
        $this->smarty->assign("strUrlDel", site_url("user/user/processDelete"));
        $this->smarty->assign("strUrlEdit", site_url("user/user/edit"));
        $strBreadcrumbHtml = "<a href='" . base_url() . "index.php/home/welcomeadmin'>Home </a>&raquo; <b>User</b>";
        $this->smarty->assign("strBreadcrumbUrl", $strBreadcrumbHtml);
        //-- notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();

        //--display pagination
        $intTotalRow = $this->m_user->count();
        $arrConfig['base_url'] = site_url("user/user/page");
        $arrConfig['total_rows'] = $intTotalRow;
        $arrConfig['per_page'] = 20;
        $arrConfig['uri_segment'] = 4;
        $arrConfig['cur_tag_open'] = "<a class='current'>";
        $arrConfig['cur_tag_close'] = '</a>';
        $this->pagination->initialize($arrConfig);
        $strPaginationLink = $this->pagination->create_links();
        $intPage = $this->uri->segment(4);

        $intLimit = $arrConfig['per_page'];
        if (!$intPage) {
            $intPage = 0;
        }

        $arrAllUser = array(intval($intPage), $intLimit);
        $arrUser = $this->m_user->getAll($arrAllUser);

        $this->smarty->assign("arrUserdata", $arrUser);
        $this->smarty->assign("strPaginationLink", $strPaginationLink);
        //-- set template content
        $this->smarty->assign("strTemplateContent", "user/list.html");
        //-- set nav active menu
        $this->smarty->assign("strNavAddStatus", "");
        $this->smarty->assign("strNavListStatus", "class='nav-active'");

        //-- output
        parent::display();
    }

    public function add() {
        //-- set rule
        $this->setPageRule("C");
        //-- set templates
        $this->smarty->assign("strTemplateContent", "user/add.html");
        //-- set url
        $this->smarty->assign("strUrlList", site_url("user/user"));
        $this->smarty->assign("strUrlAdd", site_url("user/user/add"));
        $this->smarty->assign("strUrlProcessAdd", site_url("user/user/processAdd"));
        //-- display notif
        $this->tnotification->delete_last_field();
        $this->tnotification->display_notification();
        //-- set breadcrumb
        $strBreadcrumbHtml = "<a href='" . base_url() . "index.php/home/welcomeadmin'>Home </a>&raquo; <a href='" . base_url() .
                "index.php/user/user'>User </a>&raquo; <b> Tambah User</b>";
        $this->smarty->assign("strBreadcrumbUrl", $strBreadcrumbHtml);
        //-- set nav active menu
        $this->smarty->assign("strNavListStatus", "");
        $this->smarty->assign("strNavAddStatus", "class='nav-active'");
        parent::display();
    }

    public function processAdd() {
        //-- set page rule
        $this->setPageRule("C");
        //-- check rule
        $this->tnotification->set_rules('txtUserId', 'User Id', 'trim|required|max_length[20]');
        $this->tnotification->set_rules('txtUsername', 'Nama', 'trim|required');
        $this->tnotification->set_rules('txtPassword', 'Password', 'trim|required');

        if ($this->tnotification->run() != FALSE) {
            $strRuleTp = "";
            //-- set "create" rule
            if (!$this->input->post("c") == '') {
                $strRuleTp .="1";
            } else {
                $strRuleTp .="0";
            }
            //-- set "create" rule
            if (!$this->input->post("r") == '') {
                $strRuleTp .="1";
            } else {
                $strRuleTp .="0";
            }
            //-- set "create" rule
            if (!$this->input->post("u") == '') {
                $strRuleTp .="1";
            } else {
                $strRuleTp .="0";
            }
            //-- set "create" rule
            if (!$this->input->post("d") == '') {
                $strRuleTp .="1";
            } else {
                $strRuleTp .="0";
            }
            $arrSessData = $this->session->userdata("session_admin");
            $arrSaveParams = array($this->input->post("txtUserId"),
                $this->input->post("txtUsername"),
                $this->input->post("txtPassword"),
                $this->input->post("loginSt"),
                $this->input->post("lockSt"),
                $strRuleTp,
                $arrSessData['user_id']);
            if ($this->m_user->saveAdd($arrSaveParams)) {
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        redirect("user/user/add");
    }

    public function edit($pStrUserId) {
        //-- set page rule
        $this->setPageRule("U");
        //-- set template content
        $this->smarty->assign("strTemplateContent", "user/edit.html");
        $arrUser = $this->m_user->getById($pStrUserId);
        $arrRuleName = array('Create', 'Retrieve', 'Update', 'Delete');
        $arrRule = array('c', 'r', 'u', 'd');
        $arrRuleValue = array();
        $strHtmlRule = "";
        for ($intId = 0; $intId < 4; $intId++) {
            $arrRuleValue[$intId] = substr($arrUser['rule_tp'], $intId, 1);
            if ($arrRuleValue[$intId] == 1) {
                $strHtmlRule .= "<input type='checkbox' checked='checked' value='1' name='" . $arrRule[$intId] . "'/>" . $arrRuleName[$intId];
            } else {
                $strHtmlRule .= "<input type='checkbox' value='1' name='" . $arrRule[$intId] . "'/>" . $arrRuleName[$intId];
            }
        }

        //-- display notif
        $this->tnotification->delete_last_field();
        $this->tnotification->display_notification();

        $this->smarty->assign("strUrlList", site_url("user/user"));
        $this->smarty->assign("strUrlAdd", site_url("user/user/add"));
        $this->smarty->assign("strUrlProcessEdit", site_url("user/user/processEdit"));
        //-- set breadcrumb
        $strBreadcrumbHtml = "<a href='" . base_url() . "index.php/home/welcomeadmin'>Home </a>&raquo; <a href='" . base_url() .
                "index.php/user/user'>User </a>&raquo; <b> Edit User</b>";
        //-- assign var
        $this->smarty->assign("arrUser", $arrUser);
        $this->smarty->assign("strHtmlRule", $strHtmlRule);
        parent::display();
    }

    public function processEdit() {
        //-- set page rule
        $this->setPageRule("C");
        $this->tnotification->set_rules('txtUserId', 'User Id', 'trim|required|max_length[20]');
        $this->tnotification->set_rules('txtUsername', 'Nama', 'trim|required');
        $this->tnotification->set_rules('txtPassword', 'Password', 'trim|required');

        if ($this->tnotification->run() != FALSE) {
            $strRuleTp = "";
            //-- set "create" rule
            if (!$this->input->post("c") == '') {
                $strRuleTp .="1";
            } else {
                $strRuleTp .="0";
            }
            //-- set "create" rule
            if (!$this->input->post("r") == '') {
                $strRuleTp .="1";
            } else {
                $strRuleTp .="0";
            }
            //-- set "create" rule
            if (!$this->input->post("u") == '') {
                $strRuleTp .="1";
            } else {
                $strRuleTp .="0";
            }
            //-- set "create" rule
            if (!$this->input->post("d") == '') {
                $strRuleTp .="1";
            } else {
                $strRuleTp .="0";
            }
            $arrSessData = $this->session->userdata("session_admin");
            $arrEditParams = array($this->input->post("txtUsername"),
                $this->input->post("txtPassword"),
                $this->input->post("loginSt"),
                $this->input->post("lockSt"),
                $strRuleTp,
                $arrSessData['user_id'],
                $this->input->post("txtUserId"));
            if ($this->m_user->saveEdit($arrEditParams)) {
                $this->tnotification->sent_notification("success", "Data berhasil diubah");
            } else {
                $this->tnotification->sent_notification("error", "Data gagal diubah");
            }
        } else {
            $this->tnotification->sent_notification("error", "Data gagal diubah");
        }
        redirect("user/user/edit/" . $this->input->post("txtUserId"));
    }

    public function processDelete() {
        $this->setPageRule("D");
        //-- check rule
        $this->tnotification->set_rules('chkUserId[]', 'User Id', 'trim|required');
        $strData = $this->input->post('chkUserId');
        if ($this->tnotification->run() != FALSE) {
            foreach ($strData as $val) {
                //--  bisnis proses 
                if ($this->m_user->delete($val)) {
                    $this->tnotification->sent_notification("success", "Data berhasil dihapus");
                } else {
                    $this->tnotification->sent_notification("error", "Data gagal dihapus");
                }
            }
        } else {
            $this->tnotification->sent_notification("error", "Silakan pilih data yang akan dihapus terlebih dahulu");
        }
        redirect('user/user');
    }
   
    
}
